package client;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.ArrayList;

import javax.swing.JFrame;

public class FenetreJeu extends JFrame implements KeyListener, WindowListener {
	
	private static final long serialVersionUID = 4645651273806629125L;
	private Grille grille;
	//private int id_joueur;
	private Jeu jeu;
	private Panel_connexion pan_c;

	public FenetreJeu()
	{
		super("Pacman");
		pan_c = new Panel_connexion(this);
		this.setLayout(new BorderLayout());
		this.add(pan_c, BorderLayout.WEST);
		this.grille = new Grille(new Carte(), new ArrayList<Joueur>());
		this.add(grille, BorderLayout.CENTER);
		this.setSize(800, 600);
		//this.pack();
		this.setResizable(false);
		this.addKeyListener(this);
		this.addWindowListener(this);
		this.setVisible(true);
	}
	
	public void setGrille(Grille grille) {
		this.remove(this.grille);
		//this.remove(pan_c);
		//System.out.println("Avant:\n"+this.grille.toString());
		this.grille = grille;
		this.add(grille, BorderLayout.CENTER);
		//System.out.println("Apr�s:\n"+this.grille.toString());
		//this.pack();
		//this.repaint();
		this.grille.revalidate();
		this.requestFocusInWindow();
	}

	public void keyPressed(KeyEvent e) {
		System.out.println(e.getKeyCode()+"\n");
		//G 37
		//H 38
		//D 39
		//B 40

		if (jeu.deplacerJoueurLocal(e.getKeyCode()))
		{
			/*
			try
			{
				Socket s = new Socket(InetAddress.getByName("127.0.0.1"),2332);
				ThreadEnvoie th = new ThreadEnvoie(s, nr, nl);
				System.out.println("Connexion au serveur...");
				th.start();
				th.join();
				s.close();
			}
			catch (Exception ex)
			{
				System.out.println(ex);
			}
			*/
			System.out.println("OK");
		}
		else
		{
			System.out.println("D�placement impossible");
		}
	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		//System.out.println("D�placement impossible");
	}

	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		//System.out.println("D�placement impossible");
	}

	public Jeu getJeu() {
		return jeu;
	}

	public void setJeu(Jeu jeu) {
		this.jeu = jeu;
		jeu.setFen(this);
		this.setGrille(jeu.getGrille());
	}

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		String msg;
		msg = "DEC:"+this.getJeu().getId_joueur();
		System.out.println("Deconnexion: envoi "+msg);
		ThreadEnvoiUDP t = new ThreadEnvoiUDP(this.getJeu().getAddr(), this.getJeu().getPort(), msg);
		t.run();
		if (this.getJeu()!=null && this.getJeu().getThread()!=null) {
			this.getJeu().getThread().Stopper();
		}
		System.out.println("Fermeture...");
		System.exit(0);
		
	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
